# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json
import logging
from pymongo import MongoClient
from wipo.libs import config


class WipoPipeline:

    def open_spider(self, spider):
        db_url = config.MONGO_URL
        db_port = config.MONGO_PORT
        db_name = config.MONGO_DB
        self.db_client = MongoClient(host=db_url, port=db_port)
        if spider.name == "brand":
            self.db = self.db_client[db_name].brand
        elif spider.name == "design":
            self.db = self.db_client[db_name].design

    def close_spider(self, spider):
        self.db_client.close()

    def process_item(self, item, spider):
        if spider.name == 'brand':
            latest_date = item['latestDate']
            resp = item['resp']
            docs = resp.get('docs')
            start = resp.get('start')
            sour = docs[0].get('OO')
            for d in docs:
                if docs.index(d) == 0 and start == 0:
                    latest_date = d.get('AD')
                self.db.insert(dict(d))
            val = {
                'latestDate': latest_date,
                'startFrom': int(start) + len(docs),
                'status': item['status'],
                'numFound': resp.get('numFound')
            }
            config.conn.hset('wipo-config', sour, json.dumps(val))
            logging.info('{}//startFrom:{} save ok!'.format(sour, start))
        elif spider.name == 'design':
            latest_date = item['latestDate']
            resp = item['resp']
            docs = resp.get('docs')
            start = resp.get('start')
            for d in docs:
                if docs.index(d) == 0 and start == 0:
                    latest_date = d.get('RD')
                self.db.insert(dict(d))
            val = {
                'latestDate': latest_date,
                'startFrom': int(start) + len(docs),
                'numFound': resp.get('numFound')
            }
            config.conn.hset('wipo-design', 'config', json.dumps(val))
            logging.info('design//startFrom:{} save ok!'.format(start))
        return item



